package com.putao.tinytime.activities;

import android.animation.ObjectAnimator;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.putao.tinytime.R;
import com.putao.tinytime.adapter.GalleryListAdapter;
import com.putao.tinytime.adapter.StickyGridAdapter;
import com.putao.tinytime.base.BaseActivity;
import com.putao.tinytime.bean.GalleryEntity;
import com.putao.tinytime.bean.PhotoGridItem;
import com.putao.tinytime.event.BasePostEvent;
import com.putao.tinytime.event.EventBus;
import com.putao.tinytime.util.PhotoUtil;
import com.putao.tinytime.util.PuTaoConstants;
import com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView;

import java.util.ArrayList;
import java.util.List;


public class PhotoSelectActivity extends BaseActivity implements View.OnClickListener {
    private Button back_btn, btn_gallery;
    private ListView layout_gallery_list;
    private final static int SetAlbumPhotos = 1;
    private final static int SetGalleryList = 2;
    private StickyGridHeadersGridView mGridView;
    private List<PhotoGridItem> mGirdList = new ArrayList<PhotoGridItem>();
    private LinearLayout sl_gallery_list;
    private boolean mGalleryShow = false;
    ArrayList<GalleryEntity> mGalleryList;
    private RelativeLayout gallery_list_panel;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SetAlbumPhotos:
                    LoadAlbumData();
                    break;
                case SetGalleryList:
                    loadGalleryList();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_photo_select;
    }

    @Override
    public void doInitSubViews(View view) {
        mGridView = (StickyGridHeadersGridView) findViewById(R.id.asset_grid);
        back_btn = (Button) this.findViewById(R.id.back_btn);
        btn_gallery = (Button) findViewById(R.id.btn_gallery);
        layout_gallery_list = (ListView) findViewById(R.id.layout_gallery_list);
        sl_gallery_list = (LinearLayout) findViewById(R.id.sl_gallery_list);
        gallery_list_panel = (RelativeLayout) findViewById(R.id.gallery_list_panel);
        gallery_list_panel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGalleryShow) {
                    hideGalleryLsit();
                }
            }
        });
        gallery_list_panel.setVisibility(View.GONE);
        addOnClickListener(back_btn, btn_gallery);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Loger.d("select image path:" + mGirdList.get(position).getPath());
                Bundle bundle = new Bundle();
                bundle.putString("photo_path", mGirdList.get(position).getPath());
                EventBus.getEventBus().post(new BasePostEvent(PuTaoConstants.EVENT_CODE_SELECT_PHOTO, bundle));
                finish();
                // TODO Auto-generated method stub
            }
        });
        mGridView.setHeadersIgnorePadding(true);
    }

    @Override
    public void doInitData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {


        }
        getData();
        getGallery();
    }

    void getGallery() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mGalleryList = PhotoUtil.QueryALLGalleryList(mContext);
                sendHandleMessage(SetGalleryList);
            }
        }).start();
    }


    void reSetAlbumData(final int bucket_id) {
        final ProgressDialog dialog = new ProgressDialog(mContext);
        dialog.setMessage("正在更新相册照片...");
        dialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                mGirdList = PhotoUtil.QueryPhotoByBUCKET_ID(mContext, bucket_id);
                sendHandleMessage(SetAlbumPhotos);
                dialog.dismiss();
            }
        }).start();

    }

    void loadGalleryList() {
        GalleryListAdapter galleryAdapter = new GalleryListAdapter(mContext, mGalleryList);
        layout_gallery_list.setAdapter(galleryAdapter);
        layout_gallery_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GalleryEntity entity = mGalleryList.get(position);
                reSetAlbumData(entity.getBucket_id());
                hideGalleryLsit();
            }
        });
    }

    /**
     * 执行友盟统计
     */
    private void doAnalysis() {
//        String eventcode = UmengAnalysisConstants.UMENG_COUNT_EVENT_JIGSAW_IMAGEPICKER_ONE;
//        switch (maxnum) {
//            case 1:
//                eventcode = UmengAnalysisConstants.UMENG_COUNT_EVENT_JIGSAW_IMAGEPICKER_ONE;
//                break;
//            case 2:
//                eventcode = UmengAnalysisConstants.UMENG_COUNT_EVENT_JIGSAW_IMAGEPICKER_TWO;
//                break;
//            case 3:
//                eventcode = UmengAnalysisConstants.UMENG_COUNT_EVENT_JIGSAW_IMAGEPICKER_THREE;
//                break;
//            case 4:
//                eventcode = UmengAnalysisConstants.UMENG_COUNT_EVENT_JIGSAW_IMAGEPICKER_FOUR;
//                break;
//        }
//        doUmengEventAnalysis(eventcode);
    }


    void LoadAlbumData() {
        mGridView.setAdapter(new StickyGridAdapter(mActivity, mGirdList, mGridView));
    }

    void getData() {
        final ProgressDialog dialog = new ProgressDialog(mContext);
        dialog.setMessage("正在读取照片...");
        dialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                mGirdList = PhotoUtil.QueryALLPhoto(mContext);
                sendHandleMessage(SetAlbumPhotos);
                dialog.dismiss();
            }
        }).start();
    }

    private void sendHandleMessage(int messagecode) {
        Message msg = new Message();
        msg.what = messagecode;
        mHandler.sendMessage(msg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.btn_gallery:
                if (mGalleryShow) {
                    hideGalleryLsit();
                } else {
                    showGalleryList();
                }
                break;
            default:
                break;
        }
    }

    void showGalleryList() {
        ObjectAnimator.ofFloat(sl_gallery_list, "translationX", 0, sl_gallery_list.getWidth()).setDuration(10).start();
        sl_gallery_list.setVisibility(View.VISIBLE);
        gallery_list_panel.setVisibility(View.VISIBLE);
        ObjectAnimator.ofFloat(sl_gallery_list, "translationX", sl_gallery_list.getWidth(), 0).setDuration(500).start();
        mGalleryShow = true;
    }

    void hideGalleryLsit() {
        ObjectAnimator.ofFloat(sl_gallery_list, "translationX", 0, sl_gallery_list.getWidth()).setDuration(500).start();
        mGalleryShow = false;
        gallery_list_panel.postDelayed(new Runnable() {
            @Override
            public void run() {
                gallery_list_panel.setVisibility(View.GONE);
            }
        }, 550);
    }

}
